function halfInterval(arr, target) {
    if (!arr.length) return;
    var sortedArr = arr.sort(
        function sort(a, b) {
            return a < b ? -1 : 1
        }
    );

    function search(array, start, end) {
        var mid = parseInt(((start + end) / 2) + "");
        var middle = array[mid];
        if (!array.length || (end < start)) return -1;
        console.log(mid, middle, start, end);
        if (middle < target) {
            return search(array, mid + 1, end);
        } else if (middle > target) {
            return search(array, start, mid - 1);
        } else if (middle === target) {
            return mid;
        }
    }

    return search(sortedArr, 0, sortedArr.length);
}

const a = [1, 2, 3, 4, 5, 6];

console.log('result: ', halfInterval(a, 5));
console.log('a', a);
